home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / NewFile / Source / Protos.m < prev    next >
Text File  |  1992-11-11  |  7KB  |  347 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Protos.h"
  5. #import "Proto.h"
  6. #import "Main.h"
  7. #import <appkit/Matrix.h>
  8. #import <appkit/Cell.h>
  9. #import <appkit/TextField.h>
  10. #import <objc/List.h>
  11. #import <appkit/Application.h>
  12. #import <appkit/OpenPanel.h>
  13. #import <sys/param.h>
  14.  
  15.  
  16. @implementation Protos
  17.  
  18. + new
  19. {
  20.     self = [super new];
  21.     list = [List new];
  22.     return self;
  23. }
  24.  
  25. //     --------------------------------------------------------
  26. // Abstraction of the interface.
  27.  
  28. - (char *)typename
  29. {
  30.     return (char *)[typename stringValue];
  31. }
  32. - settypename:(char *)atypename
  33. {
  34.     [typename setStringValue:atypename];
  35.     return self;
  36. }
  37.  
  38. - (char *)pathname
  39. {
  40.     return (char *)[pathname stringValue];
  41. }
  42. - setpathname:(char *)apathname
  43. {
  44.     [pathname setStringValue:apathname];
  45.     return self;
  46. }
  47.  
  48. - (char *)editor
  49. {
  50.     return (char *)[editor stringValue];
  51. }
  52. - seteditor:(char *)aneditor
  53. {
  54.     [editor setStringValue:aneditor];
  55.     return self;
  56. }
  57.  
  58. - (int)defaultopen
  59. {
  60.     return [[defaultopen selectedCell] tag];
  61. }
  62. - setdefaultopen:(int)open
  63. {
  64.     [defaultopen selectCellWithTag:open];
  65.     return self;
  66. }
  67.  
  68.  
  69. // Fill in the interface for proto.
  70. // If proto is nil, then fill in blanks.
  71. - showtypedetails:proto
  72. {
  73.     if (proto)
  74.     {
  75.         [self settypename:[proto typename]];
  76.         [self setpathname:[proto pathname]];
  77.         [self seteditor:[proto editor]];
  78.         if (strcmp([self editor], "")==0)
  79.             [openineditor setEnabled:NO];
  80.         else
  81.             [openineditor setEnabled:YES];
  82.         [self setdefaultopen:[proto defaultopen]];
  83.         [openinws setEnabled:YES];
  84.     }
  85.     else
  86.     {
  87.         [self settypename:""];
  88.         [self setpathname:""];
  89.         [self seteditor:""];
  90.         [self setdefaultopen:OPEN_EDITOR];
  91.         [openineditor setEnabled:NO];
  92.         [openinws setEnabled:NO];
  93.     }
  94.     return self;
  95. }
  96.  
  97.  
  98. // Load interface and initialize.
  99. - loadnib
  100. {
  101.     int i;
  102.     [NXApp loadNibSection:"Protos.nib" owner:self];
  103.     // Delete the dummy entries used to set up matrix in InterfaceBuilder
  104.     for (i=0; i<3; i++)
  105.         [typematrix removeRowAt:0 andFree:YES];
  106.     
  107.     // Fill in entries for prototypes.
  108.     for (i=0; i<[list count]; i++)
  109.     {
  110.         [typematrix addRow];
  111.         [typematrix setTitle:[[list objectAt:i] typename] at:i:0];
  112.     }
  113.     
  114.     [typematrix sizeToCells];
  115.     [typematrix selectCellAt:0:0];
  116.     [typematrix display];
  117.     [self showtype:self];
  118.     return self;
  119. }
  120.  
  121.  
  122. // Create a proto from the entries in the interface.
  123. - formproto
  124. {
  125.     id proto = [Proto new
  126.         :[self typename]:[self pathname]:[self editor]:[self defaultopen]];
  127.     return proto;
  128. }
  129.  
  130.  
  131. //     --------------------------------------------------------
  132.  
  133. //     --------------------------------------------------------
  134. // Respond to the interface.
  135.  
  136. // Display the interface.
  137. - showwindow:sender
  138. {
  139.     static nibloaded = 0;
  140.     
  141.     if (nibloaded==0)
  142.     {
  143.         [self loadnib];
  144.         nibloaded++;
  145.     }
  146.     [window makeKeyAndOrderFront:self];
  147.     return self;
  148. }
  149.  
  150. - openineditor:sender
  151. {
  152.     char fullname[MAXPATHLEN];
  153.     [main openineditor:[main fullprotoname:fullname:[self selectedpathname]]:[self selectededitor]];
  154.     return self;
  155. }
  156.  
  157. - openinworkspace:sender
  158. {
  159.     char fullname[MAXPATHLEN];
  160.     [main openinws:[main fullprotoname:fullname:[self selectedpathname]]];
  161.     return self;
  162. }
  163.  
  164. - remove:sender
  165. {
  166.     [self removefromlist:[self selectedname]];
  167.     return self;
  168. }
  169.  
  170. - modify:sender
  171. {
  172.     id    proto = [self formproto];
  173.     int    newcell = [self findposinlist:[proto typename]];
  174.     int    pos = [self addtolist:proto];
  175.     if (newcell<0)
  176.     {
  177.         [typematrix setTitle:[proto typename] at:pos:0];
  178.         [typematrix sizeToCells];
  179.         [typematrix display];
  180.     }
  181.     [typematrix selectCellAt:pos:0];
  182.     [remove setEnabled:YES];
  183.     [self showtype:self];
  184.     return self;
  185. }
  186.  
  187.  
  188. // Called when user clicks on entry in type list.
  189. - showtype:sender
  190. {
  191.     char    *name = [self selectedname];
  192.     id    proto = [self protoatpos:[self findposinlist:name]];
  193.     [self showtypedetails:proto];
  194.     return self;
  195. }
  196.  
  197. //     --------------------------------------------------------
  198. // The list of proto's. The list is ordered alphabetically.
  199.  
  200. // The list.
  201. - protoslist
  202. {
  203.     return list;
  204. }
  205.  
  206. // Return the proto whose typename is name.
  207. - protoforname:(char *)name
  208. {
  209.     int pos;
  210.     pos = [self findposinlist:name];
  211.     return [self protoatpos:[self findposinlist:name]];
  212. }
  213.  
  214. /*
  215. Return the position in the list of the proto with typename name.
  216. If no proto has that name, return the position at which a new entry with that name should go.
  217. */
  218. - (int)findposinlist:(char *)name
  219. {
  220.     int    len = [list count];
  221.     int i = 0;
  222.     int    compare = -1;
  223.     while (i<len && compare < 0)
  224.     {
  225.         compare = strcmp([[list objectAt:i] typename], name);
  226.         if (compare<0) i++;
  227.     }
  228.     if (compare==0)
  229.         return i;
  230.     else
  231.         return -i;
  232. }
  233.  
  234.  
  235. // The proto at position pos.
  236. - protoatpos:(int)pos
  237. {
  238.     return [list objectAt:pos];
  239. }
  240.  
  241. /*
  242. If a proto with the name typename as proto is already in the list, replace that entry (destroying the old entry), otherwise add a new entry.
  243. Return the position of the added/modified entry.
  244.  
  245. The interface is not updated: have to avoid the interface in this routine since the list is loaded and accessed on startup, and the interface is not loaded until it is required.
  246. */
  247. - (int)addtolist:proto
  248. {
  249.     id    old;
  250.     int pos = [self findposinlist:[proto typename]];
  251.     if (pos<=0)
  252.     {
  253.         // New entry.
  254.         pos = -pos;
  255.         [list insertObject:proto at:pos];
  256.         [typematrix insertRowAt:pos];
  257.     }
  258.     else
  259.     {
  260.         // Modify entry.
  261.         old = [list replaceObjectAt:pos with:proto];
  262.         [old free];
  263.     }
  264.     return pos;
  265. }
  266.  
  267.  
  268. // Remove the proto with typename name and update the interface.
  269. - (int)removefromlist:(char *)name
  270. {
  271.     int pos = [self findposinlist:name];
  272.     if (pos>=0)
  273.     {
  274.         [list removeObjectAt:pos];
  275.         [typematrix removeRowAt:pos andFree:YES];
  276.         [typematrix sizeToCells];
  277.         
  278.         // Select the entry that was previous to the deleted entry in the list.
  279.         if (pos==[typematrix cellCount]) pos--;
  280.         [typematrix selectCellAt:pos:0];
  281.         [typematrix display];
  282.     }
  283.     if ([typematrix cellCount]==0) [remove setEnabled:NO];
  284.     [self showtype:self];
  285.     return pos;
  286. }
  287.  
  288.  
  289. // Load the list.
  290. - readprotos:(FILE *)fp
  291. {
  292.     id    proto;
  293.     int    pos;
  294.     if (list) [list free];
  295.     list = [List new];
  296.     do
  297.     {
  298.         proto = [Proto read:fp];
  299.         if (proto)
  300.         {
  301.             pos = [self addtolist:proto];
  302.         }
  303.     }
  304.     while (proto);
  305.     return self;
  306. }
  307.  
  308.  
  309. // Store the list.
  310. - writeprotos:(FILE *)fp
  311. {
  312.     int i;
  313.     int count = [list count];
  314.     for (i=0; i<count; i++)
  315.         [[list objectAt:i] write:fp];
  316.     return self;
  317. }
  318.  
  319. //    --------------------------------------------------------
  320.  
  321. // Properties of the proto whose name is currently selected in the type list.
  322.  
  323. // The proto itself.
  324. - selected
  325. {
  326.     char *name = (char *)[[typematrix selectedCell] title];
  327.     if (strcmp(name, "") == 0)
  328.         return nil;
  329.     else
  330.         return [self protoatpos:[self findposinlist:name]];
  331. }
  332.  
  333. - (char *)selectedname
  334. {
  335.     return (char *)[[typematrix selectedCell] title];
  336. }
  337. - (char *)selectededitor
  338. {
  339.     return [[self selected] editor];
  340. }
  341. - (char *)selectedpathname;
  342. {
  343.     return [[self selected] pathname];
  344. }
  345.  
  346. @end
  347.